﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared;
using Sheego.Service.X400.Job.Shared.BO;
using Sheego.Service.X400.Job.Shared.Locator;
using Sheego.Service.X400.Job.Shared.Processor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl.Processor
{
    class SendMailProcessor : IX400MessageProcessor
    {
        private string Recipient = null;


        private IMailManager MailManager;
        private IFileManager FileManager;

        public SendMailProcessor(IMailManager mailManager, IFileManager fileManager)
        {
            MailManager = mailManager;
            FileManager = fileManager;
        }
     

        #region IX400MessageProcessor Members
        public void Process(IX400Message Message)
        {
            if (FileManager == null)
                throw new ArgumentNullException("FileManager");

            if (Message == null)
                throw new ArgumentNullException("Message");



            var Mail = JobLocator.GetMailMessage();


            Mail.SubjectType = "New X.400 Message";
            Mail.SubjectDetail = Message.P2Originator.ToString();

            Mail.Recipients.Add(Recipient);


           


            int cnt = 0;
            //TODO WriteHeader
            StringBuilder text = new StringBuilder();

            text.AppendFormat("P2Originator   : {0}\r\n", Message.P2Originator);
            text.AppendFormat("P7Originator   : {0}\r\n", Message.P7Originator);
            text.AppendFormat("Subject        : {0}\r\n", Message.Subject);
            text.AppendFormat("SubmissionTime : {0}\r\n", Message.SubmissionTime);
            text.AppendFormat("CreationTime   : {0}\r\n", Message.CreationTime);

            foreach (var MsgPart in Message.MessageParts)
            {

                var attachment = JobLocator.GetMailAttachment();
              

                //Attach MessagePart
                var PartSubType = "Part";
                var PartExtension = "bin";


                if (Constants.ContentTypeEdifact.Equals(MsgPart.ContentType.MIMEType))
                {
                    PartSubType = MsgPart.ContentType.SubType;
                    PartExtension = "EDI";
                }
                
                
                attachment.Name = String.Format("{0}_{1}.{2}", PartSubType, cnt, PartExtension);

                attachment.FilePath = FileManager.CreateTempFileName(attachment.Name);
                FileManager.WriteFile(attachment.FilePath, MsgPart.Content);
                text.AppendFormat("MessagePart{0}-ContentType   : {1}\r\n", cnt, MsgPart.ContentType.MIMEType);
                text.AppendFormat("MessagePart{0}-ContentLength : {1}\r\n", cnt, MsgPart.ContentLength);
                text.AppendFormat("MessagePart{0}-SubType   : {1}\r\n", cnt, MsgPart.ContentType.SubType);

                Mail.Attachments.Add(attachment);

                cnt++;

            }

            Mail.Body = text.ToString();
            MailManager.Send(Mail);
        }

        public void Config(string Key, string Value)
        {
            if (string.IsNullOrWhiteSpace(Key))
                throw new ArgumentNullException("Key");
            if (string.IsNullOrWhiteSpace(Value))
                throw new ArgumentNullException("Value");

            //email + server
            if (Key.Equals("Recipient"))
            {
                Recipient = Value;
                return;
            }
         
            throw new KeyNotFoundException(Key);
        }

        #endregion
    }
}
